package com.zxbit.flash.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

import java.util.List;

public class LocationUtils {

    private static final String TAG = LocationUtils.class.getSimpleName();

    @SuppressLint("MissingPermission")
    public static Location getLocation(Context context) {
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = manager.getProviders(true);
        if (providers == null) return null;
        Location lastKnownLocation = null;


        if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            lastKnownLocation = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        if (lastKnownLocation == null && providers.contains(LocationManager.GPS_PROVIDER)) {
            lastKnownLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        }
        if (lastKnownLocation == null && providers.contains(LocationManager.PASSIVE_PROVIDER)) {
            lastKnownLocation = manager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        }

        return lastKnownLocation;
    }

}
